


// 数据库名 暂时只会用到一个库
import 'package:flutter_pleasure_video/data/database/yck_rule_helper.dart';
import 'package:flutter_pleasure_video/util/extensions/string_extensions.dart';
import 'package:sqflite/sqflite.dart';

const String _DB_NAME = "pleasure_video.db";
// 数据库版本 从1开始
const int _DB_VERSION = 1;

//TODO 需要考虑是否需要单例，每次操作都要开关DB是否必要
class DBHelper {
  static DBHelper? _instance;
  late Database _db;

  static DBHelper get instance {
    if (_instance == null) {
      _instance = DBHelper();
    }
    return _instance!;
  }

  Database get db => _db;

  initDB() async {
    _db = await instance._openDB();
  }

  Future<Database> _openDB() async {
    return await openDatabase(_DB_NAME, version: _DB_VERSION,
        onUpgrade: (Database db, int oldVersion, int newVersion) async {
          "数据库$_DB_NAME需要更新：${oldVersion}=>${newVersion}".print;
          await _updateDB(db, oldVersion, newVersion);
        }, onCreate: (Database db, int version) async {
          "数据库$_DB_NAME需要创建：${version}".print;
          await _createDB(db, version);
        });
  }

  closeDB() {
    return _closeDB(_db);
  }
}

Future<void> _closeDB(Database db) async {
  return await db.close();
}


//TODO 创建数据库的方法需要根据需要写好
Future<void> _createDB(Database db, int version) async {

  //YckRuleTable
  await db.execute("create table ${YckRuleHelper.tableName} (" +
      "${YckRuleHelper.yckRuleModelId} varchar(256) primary key," +
      "${YckRuleHelper.yckRuleMode} varchar(256) not null," +
      "${YckRuleHelper.yckRuleModelTitle} varchar(256) not null," +
      "${YckRuleHelper.yckRuleModelAddTime} varchar(256) not null" +
      ")");
}


Future<void> _updateDB(Database db, int oldVersion, int newVersion) async {}